package com.ywb.controller;



import com.ywb.pojo.Result;
import com.ywb.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

//    原始存储
    /*@PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        log.info("文件上传：{},{},{}",name,age,originalFilename);
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newfilename= UUID.randomUUID().toString()+extension;
        file.transferTo(new File("C:\\4399\\"+newfilename));
        return Result.success();
    }*/

//    阿里云存储
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传：{}",file.getOriginalFilename());
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功：{}",url);
        return Result.success(url);
    }
}
